<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>

<div id="updateDialog" class="container-fluid">
    <div class="row">
        <form id="updateForm" method="post" class="form-horizontal" >
            <input type="text" hidden name="agentId" value="${umbrellaHardwareBatch.agentId}" />
            <div class="form-group">
                <label class="col-sm-4 control-label" for="code">
                    <span class="help-inline"><font color="red">*</font> </span>
                    批次号：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="code" name="code" placeholder="批次号" value="${umbrellaHardwareBatch.code}" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="costPrice" >
                    <span class="help-inline"><font color="red">*</font> </span>
                    成本价(元)：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="costPrice" name="costPrice" placeholder="成本价" value="${umbrellaHardwareBatch.costPrice}"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="marketPrice" >
                    <span class="help-inline"><font color="red">*</font> </span>
                    市场价(元)：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="marketPrice" name="marketPrice" placeholder="市场价" value="${umbrellaHardwareBatch.marketPrice}"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="brand" >
                    <span class="help-inline"><font color="red">*</font> </span>
                    品牌：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="brand" name="brand" placeholder="品牌" value="${umbrellaHardwareBatch.brand}"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="material" >
                    <span class="help-inline"><font color="red">*</font> </span>
                    材质：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="material" name="material" placeholder="材质" value="${umbrellaHardwareBatch.material}"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="weights" >
                    <span class="help-inline"><font color="red">*</font> </span>
                    净重(kg)：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="weights" name="weights" placeholder="净重(kg)" value="${umbrellaHardwareBatch.weights}"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="size" >
                    <span class="help-inline"><font color="red">*</font> </span>
                    尺寸(cm)：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="size" name="size" placeholder="尺寸(cm)" value="${umbrellaHardwareBatch.size}"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="color" >
                    <span class="help-inline"><font color="red">*</font> </span>
                    颜色：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="color" name="color" placeholder="颜色" value="${umbrellaHardwareBatch.color}"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="status" >
                    <span class="help-inline"><font color="red">*</font> </span>
                    状态：</label>
                <div class="col-sm-5">
                    <select id="status" name="status" class="form-control">
                        <option value="1" <c:if test="${umbrellaHardwareBatch.status==1}">selected="selected"</c:if>>启用</option>
                        <option value="0" <c:if test="${umbrellaHardwareBatch.status==0}">selected="selected"</c:if>>>禁止</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="description">描述：</label>
                <div class="col-sm-5">
                    <textarea class="form-control" id="description" name="description" placeholder="描述" value="${umbrellaHardwareBatch.description}"></textarea>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-9 col-sm-offset-4">
                    <button type="button" class="btn btn-primary" onclick="createSubmit();">
                        保存
                    </button>
                    <button type="button" class="btn btn-default"
                            onclick="updateDialog.close();">取消
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    function validateForm(){
        return $( "#updateForm" ).validate( {
            rules: {
                costPrice: "required",
                marketPrice: "required",
                brand: "required",
                type: "required",
                material: "required",
                weights: "required",
                size: "required",
                color: "required",
                status: "required",
                code: {
                    required: true,
                    maxlength: 50
                },
                description: {
                    maxlength: 500
                }
            },
            messages: {
                costPrice: "成本价是必填项。",
                marketPrice: "市场价是必填项。",
                brand: "品牌是必填项。",
                type: "类别是必填项。",
                material: "材质是必填项。",
                weights: "净重(kg)是必填项。",
                size: "尺寸(cm)是必填项。",
                color: "颜色是必填项。",
                status: "状态是必填项。",
                code: {
                    required: "批次号是必填项。",
                    maxlength: "名称长度超出50个字符。"
                },
                description: {
                    maxlength: "描述长度超出500个字符"
                }
            },
            errorElement: "em",
            errorPlacement: function ( error, element ) {
                // Add the `help-block` class to the error element
                error.addClass( "help-block" );

                if ( element.prop( "type" ) === "checkbox" ) {
                    error.insertAfter( element.parent( "label" ) );
                } else {
                    error.insertAfter( element );
                }
            },
            highlight: function ( element, errorClass, validClass ) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" );
            },
            unhighlight: function (element, errorClass, validClass) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" );
            }
        } );
    }
    //注册表单验证
    $(validateForm());

    <%--$(function(){--%>
        <%--//初始化树形select控件--%>
        <%--$("#agent").bootstrapCombotree({--%>
            <%--data: ${upmsAgents},//data应符合实例的data格式--%>
            <%--defaultLable:"请选择",--%>
            <%--name : 'pid'//combotree值域的name，可以用在表单提交--%>
        <%--})--%>
    <%--});--%>

    function createSubmit() {
        if(validateForm().form()){
            $.ajax({dataType: 'json',
                type: 'post',
                url: '${basePath}/manage/batch/update/${umbrellaHardwareBatch.id}',
                data: $('#updateForm').serialize(),
                success: function(result) {
                    if (result.code != 1) {
                        if (result.data instanceof Array) {
                            $.each(result.data, function(index, value) {
                                infoDialog(value.errorMsg);
                            });
                        } else {
                            infoDialog(result.data);
                        }
                    } else {
                        updateDialog.close();
                        $table.bootstrapTable('refresh');
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    infoDialog(textStatus);
                }
            });
        }
    }
</script>